package pers.mihao.quickstream.publish.model.dto;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import pers.mihao.quickstream.common.task.domain.scheduler.InvokeFailRetryTaskTimer;
import pers.mihao.quickstream.common.task.domain.scheduler.AutomaticTaskTimer;
import pers.mihao.quickstream.publish.constants.PublishTimerType;

import java.util.Objects;
import java.util.Optional;

/**
 * @author mihao
 * @since 2024/11/22
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PublishTimeConfig {

    private String type;

    private String publishTime;

    private Integer retryNum;

    public AutomaticTaskTimer parseTimeoutTaskTimer() {
        PublishTimerType publishTimerType = Objects.requireNonNull(PublishTimerType.of(type));
        AutomaticTaskTimer taskTimer = null;
        switch (publishTimerType) {
            case NOW:
                taskTimer = new InvokeFailRetryTaskTimer(System.currentTimeMillis(), retryNum);
                break;
            case TIMER:
                DateTime dateTime = DateUtil.parseDateTime(publishTime);
                taskTimer = new InvokeFailRetryTaskTimer(dateTime.getTime(), retryNum);
                break;
        }
        return taskTimer;
    }

}
